<template>
  <div class="puublic-event">
    <div class="image">
      <div
        class="nav"
        @click="this.$router.go(-1)"
        :style="{ backgroundColor: nav_show ? '#fff' : '', color: nav_show ? '#333' : '#fff' }"
      >
        <van-icon name="arrow-left" /><span>热心公益</span>
      </div>
      <img :src="eventlist.pic ? 'http://192.168.101.65:9000' + eventlist.pic : pic" alt="" />
    </div>
    <div class="profile">
      <p class="title">{{ eventlist.name }}</p>
      <p class="count">可获得积分：{{ eventlist.point }} 分</p>
      <p class="count">参与人数：{{ eventlist.person }} 人</p>
    </div>
    <div class="introduce">
      <p class="introduce-title">项目介绍</p>
      <p class="introduce-content">
        {{ eventlist.remark }}
      </p>
    </div>
    <div class="join">
      <van-button type="danger" @click="joinPublic(this.params.eventId)">参与此公益</van-button>
    </div>
  </div>
</template>

<script>
import { showDialog, showToast } from 'vant'
import { ContentList } from '../../api/public'
import { getAddPublic } from '../../api/user'
export default {
  data() {
    return {
      pic: require('../../assets/image/default.png'),
      nav_show: false,
      eventlist: [],
      params: []
    }
  },
  created() {
    this.getlist()
    this.params = {
      cateId: this.$route.params.cateId,
      eventId: this.$route.params.eventId
    }
  },
  methods: {
    handleScroll() {
      const scrollTop = document.documentElement.scrollTop
      if (scrollTop) {
        this.nav_show = true
      } else {
        this.nav_show = false
      }
    },
    async getlist() {
      const { data: res } = await ContentList()
      if (res.code === 1) {
        this.eventlist = res.data
          .find((item1) => item1.id === this.params.cateId)
          .childrenTreeNodes.find((item2) => item2.id === this.params.eventId)
      } else {
        return showToast('获取数据有误')
      }
    },
    // 参与公益
    async joinPublic(id) {
      const curId = { niceId: id }
      const { data: res } = await getAddPublic(curId)
      showDialog({
        message: res.data
      })
    }
   },
  mounted() {
    window.addEventListener('scroll', this.handleScroll)
  },
  // 组件销毁前
  beforeunmounted() {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="less" scoped>
.puublic-event {
  width: 100%;
  padding-bottom: 50px;
  background-color: #333;

  .image {
    width: 100%;
    height: 280px;

    .nav {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      padding: 10px;
      transition: all 1.2s;
      color: #fff;

      span {
        margin-left: 5px;
      }
    }

    img {
      width: 100%;
      height: 100%;
    }
  }

  // 公益简介
  .profile {
    background: linear-gradient(#f2d5c3, #f6e3d7);
    margin: 10px;
    padding: 10px;
    border-radius: 10px;

    p {
      margin: 6px 0;
    }

    .title {
      font-weight: 600;
    }

    .label {
      font-size: 13px;
      color: #d47336;

      span {
        display: inline-block;
        padding: 2px 5px;
        border: 1px solid #d47336;
        border-radius: 4px;

        &:nth-child(2) {
          margin-left: 6px;
        }
      }
    }

    .call {
      color: #666;
      font-size: 14px;
    }

    .count {
      font-size: 14px;
    }
  }

  // 项目内容
  .introduce {
    margin: 12px 10px;
    padding: 10px;
    background-color: #fff;
    border-radius: 10px;

    .introduce-title {
      font-weight: 600;
      margin: 0;
    }

    .introduce-content {
      line-height: 27px;
      font-size: 15px;
      text-indent: 2em;
    }
  }

  // 参与
  .join {
    display: flex;
    align-items: center;
    position: fixed;
    bottom: 0;
    left: 0;
    background-color: #fff;
    width: 100%;
    height: 50px;
    padding: 5px 10px;
    box-sizing: border-box;

    .van-button {
      width: 100%;
      height: 100%;
      margin: auto;
    }
  }
}
</style>
